package createadmin2;

## This file is part of the Simplemail project.
#  Copyright (c) 2008 the Simplemail Team (see authors).
# 
#  For the full copyright and license information, please view the COPYING
#  file that was distributed with this source code. You can also view the
#  COPYING file online at http://unixforum.org.ua/misc/COPYING.txt
# 
#  @copyright  (c) the authors
#  @author     Dmitriy Shilenko <rtyug@ukr.net>
#  @license    GPLv2 http://unixforum.org.ua/misc/COPYING.txt
#  @package    Panel Mailbox
#  @version    $Id: $

use strict;
use CGI::Carp qw(fatalsToBrowser);
use Data::Validate::Domain qw(is_domain);
use base 'func', 'createadmin';

BEGIN {
    require 'func.pm';
}

my $func = func->new();

sub new {
    my $self = shift->SUPER::new(@_);
    $self->dbi_connect if $self->can("dbi_connect");
    return $self;
}

sub valid_param_createadmin {
    my $self = shift;

    if (   !length( $self->{p}->{fUsername} )
        || 100 < length( $self->{p}->{fUsername} )
        || $self->{p}->{fUsername} !~ /[A-Za-z0-9]/ )
    {
        $self->{he}->{fUsername_correct} = 1;
    }

    if ( $self->{p}->{fDomains} ) {
        my ( $item, $found, $index );
        foreach $_ ( split( "\0", $self->{p}->{fDomains} ) ) {
            if ( !is_domain($_) ) { $found = 1; $index = $_; last }
        }

        if ($found) { $self->{he}->{fDomains_no_correct} = 1 }

        if ( !$found ) {
            my ( $item, $found );
        foreach $_ ( split( "\0", $self->{p}->{fDomains} ) ) {

                my $sth = $self->{dbh}->prepare(
                    'SELECT domain
                                FROM domain 
                                WHERE domain = ? 
                                LIMIT 1
                             '
                );
                $sth->execute($_);
                my $username_sql = $sth->fetchrow_array();
                $sth->finish();

                if ( !defined($username_sql) ) { $found = 1; last }
            }
            if ($found) { $self->{he}->{fDomains_no_exist} = 1; }
        }

    }
    else {
        $self->{he}->{no_fDomains} = 2;
    }

    if ( $self->{p}->{fCreateddomain} eq 'on' ) {
        $self->{p}->{fCreateddomain} = 1;
        if ( $self->{p}->{fCreatedalias} ne 'on' ) {
            $self->{p}->{fMaxalias} = -1;
        }
        else {
            if ( 5 < length( $self->{p}->{fMaxalias} )
                || $self->{p}->{fMaxalias} !~ /^\d+$/ )
            {
                $self->{he}->{fMaxalias_correct} = 1;
            }
        }

        if ( 5 < length( $self->{p}->{fMaxmailbox} )
            || $self->{p}->{fMaxmailbox} !~ /^\d+$/ )
        {
            $self->{he}->{fMaxmailbox_correct} = 1;
        }
        if ( 5 < length( $self->{p}->{fMaxdomain} )
            || $self->{p}->{fMaxdomain} !~ /^\d+$/ )
        {
            $self->{he}->{fMaxdomain_correct} = 1;
        }

        if ( 5 < length( $self->{p}->{fMaxsizedomain} )
            || $self->{p}->{fMaxsizedomain} !~ /^\d+$/ )
        {
            $self->{he}->{fMaxsizedomain_correct} = 1;
        }
        if ( 5 < length( $self->{p}->{fMaxsizemailbox} )
            || $self->{p}->{fMaxsizemailbox} !~ /^\d+$/ )
        {
            $self->{he}->{fMaxsizemailbox_correct} = 1;
        }

        if (   $self->{he}->{fMaxmailbox_correct} != 1
            && $self->{he}->{fMaxalias_correct} != 1 )
        {

            if (   $self->{p}->{fMaxalias} < $self->{p}->{fMaxmailbox}
                && $self->{p}->{fCreatedalias} eq 'on' )
            {
                $self->{p}->{fMaxalias} = $self->{p}->{fMaxmailbox};
            }
        }

    }
    else {
        $self->{p}->{fCreateddomain} = 0;
        if ( $self->{he}->{no_fDomains} == 2 ) {
            $self->{he}->{no_fDomains_error} = 1;
        }
    }

    if ( $self->{he}->{fMaxsizedomain_correct} != 1 ) {
        $self->{p}->{fMaxsizedomain} = $self->{p}->{fMaxsizedomain} * 1048576;
    }

    if ( $self->{he}->{fMaxsizemailbox_correct} != 1 ) {
        $self->{p}->{fMaxsizemailbox} = $self->{p}->{fMaxsizemailbox} * 1048576;
    }

    return $self;
}

sub valid_param_createadmin2 {
    my $self = shift;

    if (   ( !length( $self->{p}->{fPassword} ) )
        || ( 100 < length( $self->{p}->{fPassword} ) )
        || (
            $self->{se}->{max_long_passwd} > length( $self->{p}->{fPassword} ) )
      )
    {
        $self->{he}->{fPass_correct} = 1;
    }

    if ( $self->{p}->{fPassword} ne $self->{p}->{fPassword2} ) {
        $self->{he}->{fPass2_correct} = 1;
    }

    if ( $self->{he}->{fUsername_correct} != 1 ) {

        my $sth = $self->{dbh}->prepare(
            'SELECT name
                         FROM users 
                         WHERE name = ? 
                         LIMIT 1  
                             '
        );
        $sth->execute( $self->{p}->{fUsername} );
        my $username_sql = $sth->fetchrow_array();
        $sth->finish();

        if ( defined($username_sql)
            && ( $username_sql eq $self->{p}->{fUsername} ) )
        {
            $self->{he}->{fUsername_correct_exist} = 1;
        }
    }

    return $self;
}

sub insert_admin {
    my $self = shift;

    my $fount;
    foreach my $value ( values( %{ $self->{he} } ) ) {
        if ( $value == 1 ) { $fount = 1; last; }
    }
    if ( !$fount ) {

        $self->{p}->{fPassword} = $func->md5crypt( $self->{p}->{fPassword} );

        my $sth = $self->{dbh}->prepare( '
          INSERT INTO users (name,
                             pass,
                             info,
                             active,
                             created) 
              VALUES (?,?,?,?,DATETIME("NOW"))
               ' );
        $sth->execute(
            $self->{p}->{fUsername},
            $self->{p}->{fPassword},
            $self->{p}->{fCreateddomain}, '1'
        );
        $sth->finish();

        my $lastid = $self->{dbh}->{mysql_insertid};
        if ( $self->{he}->{fUsername_no_exist} != 1 ) {

            #my $item;
        foreach $_ ( split( "\0", $self->{p}->{fDomains} ) ) {
                my $sth = $self->{dbh}->prepare(
                    'UPDATE domain
                                  SET idnane = ?
                                  WHERE domain = ?
                         '
                );
                $sth->execute( $lastid, $_ );
                $sth->finish();
            }
        }

        if ( $self->{p}->{fCreateddomain} == 1 ) {

            my $sth = $self->{dbh}->prepare( '
              INSERT INTO admin_domain (idname,
                                        m_alias, 
                                        m_mailbox, 
                                        m_domain, 
                                        mb_domain, 
                                        mb_mailbox) 
              VALUES (?,?,?,?,?,?)
               ' );
            $sth->execute(
                $lastid,                      $self->{p}->{fMaxalias},
                $self->{p}->{fMaxmailbox},    $self->{p}->{fMaxdomain},
                $self->{p}->{fMaxsizedomain}, $self->{p}->{fMaxsizemailbox}
            );
            $sth->finish();
        }

        $self->{he}->{admin_added} = 1;

    }

    return $self;
}

sub return_create_admin2 {
    shift->SUPER::return_create_admin2(@_);
}

sub templ_hash_admin {
    my $self = shift;

    $self->{session}->{filet} = 'create-admin';
    $self->SUPER::templ_hash(@_);
}

sub DESTROY {
    my $self = shift;
    $self->SUPER::DESTROY if $self->can("SUPER::DESTROY");
}

1;
